package com.numberone.web.controller.address;

import com.numberone.common.page.TableDataInfo;
import com.numberone.framework.web.base.BaseController;
import com.numberone.system.domain.Address;
import com.numberone.system.service.IAddressService;
import com.numberone.system.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 收货地址 信息操作处理
 *
 * @author 郭财源
 * @date 2020-09-27
 */
@Controller
@RequestMapping("/api/address")
public class AddressApiController extends BaseController{

    @Autowired
    private IAddressService addressService;

    /**
     * 获取用户收货地址信息
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping("/getAddressByUser")
    public Object getAddressByUser(Long userId)
    {
        Address address = new Address();
        address.setUserId(userId);
        List<Address> addresses = addressService.selectAddressList(address);
        TableDataInfo dataTable = getDataTable(addresses);
        return JsonResult.success(dataTable);
    }

    /**
     * 添加收货地址信息
     * @param address
     * @return
     */
    @ResponseBody
    @PostMapping("/addAddress")
    public JsonResult addAddress(Address address)
    {
        int i = addressService.insertAddress(address);
        if (i>0)
        {
            return JsonResult.success();
        }else {
            return JsonResult.nodata();
        }
    }

//    /**
//     * 设为默认地址
//     * @param addressId
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/editAddressDefault")
//    public JsonResult editAddressDefault(Long addressId)
//    {
//        Address address = new Address();
//        address.setId(addressId);
//        address.setDefaultAddress(1);
//        int i = addressService.updateAddress(address);
//        if (i > 0){
//            return JsonResult.success();
//        }
//        return JsonResult.nodata();
//    }

    /**
     * 修改收货地址信息
     * @param address
     * @return
     */
    @ResponseBody
    @PostMapping("/editAddress")
    public JsonResult editAddress(Address address)
    {
        int i = addressService.updateAddress(address);
        if (i>0)
        {
            return JsonResult.success();
        }else {
            return JsonResult.nodata();
        }
    }

    /**
     * 删除收货地址信息
     * @param addressId
     * @return
     */
    @ResponseBody
    @GetMapping("/deleteAddress")
    public JsonResult deleteAddress(Long addressId)
    {
        int i = addressService.deleteAddressById(addressId);
        if (i>0)
        {
            return JsonResult.success();
        }else {
            return JsonResult.nodata();
        }
    }

    /**
     * 获取用户默认收货地址
     * @param userId
     * @return
     */
    @ResponseBody
    @PostMapping("/selectAddressDefault")
    public Object selectAddressDefault(Long userId)
    {
        Address address = new Address();
        address.setStatus(1);
        address.setUserId(userId);
        List<Address> addresses = addressService.selectAddressList(address);
        TableDataInfo dataTable = getDataTable(addresses);
        return JsonResult.success(dataTable);
    }
}
